<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="zhupeng">
<TITLE>卡片出库</TITLE>
<script language="javascript" src="../script/common.js" type="text/javascript"></script>
<script type="text/javascript" src="../script/print.js"></script>
<script language="javascript" type="text/javascript">
			//全局变量
			var readOnly = false;		//明细查看
			var org = Common.cOrg();	//当前单位	
			var qt = {};
			var qt1 = {};				//数据表格对象
			var qt2 = {};
			
			var cardStatus;
			var cardTypeId;
			var cardTypeName;
			var cqtObj;
			var cardBill
			var dest;
			var subType;
			var butCancel ;
			var printDispaly;
			var adjustOutPack
			var originStoreStatus;
			//页面控件初始化
			function load(){ 
				Eui.showNavigation();
				setPages(["datalist","requestlist","printBillDetail"]);	
		
				butCancel = getPara("butCancel")
				printDispaly = getPara("printDispaly")
				if(butCancel){
					$("butCancel").innerHTML = butCancel
				}
					readOnly = getPara("readOnly") ;
				
				refreshPage()
			 	
			}
			function printBill(){
					parent.printBill(cardBill)
					parent.switchPage("printBillDetail")
			}
		
			function refreshPage(){
				var printBill = getPara("printBill")
			
				var cardBillId = getPara("cardBillId")
				DWR.call("cardStoreService.getBillInfo",cardBillId,function(result){
					cardBill = result.cardBill;
					cardStatus = result.cardStatus;
					cardTypeName = result.cardType.name;
					cardTypeId = result.cardType.id
					dest = result.dest;
					subType = cardBill.subType;
					originStoreStatus = cardBill.originStoreStatus;
					adjustOutPack = result.adjustOutPack?result.adjustOutPack:0;
					if(printDispaly && (cardBill.status == CardBillStatus.ACCPECTED || cardBill.status == CardBillStatus.PASSAGE || cardBill.status == CardBillStatus.CANCELED)){
						$("butCancel").innerHTML="<button type='button' onclick='printBill()'>打印单据</button>"
					}
					
					if(dest.parent != org.id)
						$("tab_request").style.display='none';
						
					showDetail(result)
					if(subType != CardBillSubType.IN_BUSINESS && subType != CardBillSubType.OUT_BUSINESS){
						$("tab_base").style.display='';
						createTable()
					}else{
						$("tab_base").style.display='none';
					}
					if(subType == CardBillSubType.OUT_BUSINESS){
						$("td_operater").style.display='none';
					}
				},{blockScreen:true})
				switchPage("datalist")
			}
			function updataName(dataMap){
				for(var i=0; i<dataMap.length; i++){
					for(prop in dataMap[i]){
						if(prop == "toJSONString")continue;
						$(prop).innerHTML = dataMap[i][prop]
					}
				}
			}
		
			function showDetail(result){
					Rpc.loadPage("detailedView","/store/card_bill_div.html",function(){
						var bill = result.cardBill
						
						if(bill.adjustedId){
								bill.title="在途卡片调整"
						}else{
							bill.title=Common.getText('cardbill_type',bill.billType)
						}
					
						bill.operName = result.operName
						bill.amount = Math.abs(bill.amount)
					
						bill.cardType = result.cardType.name
					
						bill.location = result.location
						if(bill.relatedId) {
							bill.pairId = bill.relatedId
						}
						
						if (bill.billType==Constants.CARD_BILL_TYPE_IN){
							var dataMap = [{destLabel:"入库来源"},{recordTimeLabel:"入库时间"},{operatorLabel:"入 库 人"},{acquireTimeLabel:"出库时间"},{acquireOperLabel:"出 库 人"}]
							updataName(dataMap)
							if(cardBill.subType == CardBillSubType.IN_BUSINESS || cardBill.subType == CardBillSubType.OUT_BUSINESS){
								bill.asn = result.asn
								$('cardbill_detail_asn').style.display=''
								$('cardbill_detail_in').style.display='none'
								
							}
							if(bill.resultFileName){
								$('result_file_name').style.display=''
							}
							if(bill.status == CardBillStatus.CANCEL || bill.status == CardBillStatus.CANCELED){
								$('cardbill_detail_pairId').style.display=''
							}
							$('cardbill_detail_adjust').style.display='none'
							bill.dest = dest.name
						}else if(bill.billType==Constants.CARD_BILL_TYPE_ADJUST){
							$('dest_td').style.display='none'
							$('cardbill_detail_in').style.display='none'
							$('cardbill_detail_canceltr').style.display='none'
							//$('cardbill_detail_pairId').style.display=''
							$('cardbill_adjust_pairId').style.display=''
							
							$('recordTimeLabel').innerHTML='调整时间'
							$('operatorLabel').innerHTML='调整人'
							$('cardbill_detail_description').innerHTML="<label>调整原因</label> <span id='description'></span>"
							
							if(bill.status == Constants.CARDBILL_STATUS_CANCELED){
								$('cardbill_detail_canceltr').style.display=''
							}
							bill.originStoreStatus = Common.getText('cardstore_status',bill.originStoreStatus)
							bill.targetStoreStatus = Common.getText('cardstore_status',bill.targetStoreStatus)
						}else{
							bill.dest = dest.name
							if(cardBill.subType == CardBillSubType.IN_BUSINESS || cardBill.subType == CardBillSubType.OUT_BUSINESS){
								subTypeId = cardBill.subType;
								bill.asn = result.asn
								$('cardbill_detail_asn').style.display=''
								$('cardbill_detail_in').style.display='none'
									
							}
							$('cardbill_detail_adjust').style.display='none'
							if(bill.status == CardBillStatus.CANCEL || bill.status == CardBillStatus.CANCELED){
								$('cardbill_detail_pairId').style.display=''
							}
							
						}
						bill.cardStatus = Common.getText('card_status',result.cardStatus)
						bill.status = Common.getText('cardbill_status',bill.status)
						bill.billTypeName = Common.getText('cardbill_type',bill.billType)
						bill.subType = Common.getText('cardbill_subtype',bill.subType)
						
						if(!readOnly)
							bill.operateBut = "<button type='button' onclick='updata()'>修 改</button>";
						
						bill.acquireTime = Format.formatToMM(bill.acquireTime);
						bill.acquireOper = result.acquireOper
						bill.recordTime = Format.formatToMM(bill.recordTime);
						bill.operator = result.operator
						bill.cancelTime = Format.formatToMM(result.cancelTime)
						bill.cancelOper = result.cancelOper
						
						Common.showDetail('cardbill_detail_main',bill);
					
						if (bill.cancelOper){
					
							$('cardbill_detail_in').style.display='none'
							
						}
						else
						{
							$('cardbill_detail_canceltr').style.display='none'
						
						}
							
					})
			}
			
			function createTable(){
				var columns=[];
				columns[0] ={type:"checkbox"}
				columns[1] = {defOrder:"asc"}
				var button 
				if(readOnly) {
					button = {
						buttons:[]
					}
				} else {
					button = {
						buttons:[
							{value:"增 加", actions:addCardNumber},
							{value:"删 除", actions:onDeleteCardNumber}
						]
					}
				}
					
				qt = new QueryTable("add_card_number",null,"datalist",columns,button);
				qt.paramFunc = function(){return {cardPack:cardBill.pack}};
				qt.showTable();
				cqtObj = qt;
			}
			function addCardNumber(){
				var opts = {}
				opts.cardBillId = cardBill.id;
				opts.cardStatus = cardStatus;
				opts.cardTypeName = cardTypeName;
				opts.billType = cardBill.billType;
				opts.targetOrg = dest.id;
				opts.targetOrgName = dest.name;
				opts.subType = subType;
				opts.originStoreStatus = originStoreStatus;
				opts.adjustedId = cardBill.adjustedId;
				opts.adjustOutPackId = adjustOutPack;
				opts.cardTypeId = cardTypeId;
				var url = spellUrl("/store/add_card_number.html",opts)
				var v = openDialogFrame(url,'请输入卡号段',200,560);
				if(v){
					refreshPage()
				}
			}
			function onDeleteCardNumber(){
				var ids = qt.getBoxValue();
				if(!ids){
			   		showWarning("请选择需要删除的卡号段"); 
			   	}else{
			   	 	if(showQuestion("你确认要删除这些卡号段吗?")==true){
					 	 ids =  ids.split(",")
			             DWR.call('cardStoreService.deleteCardNumbers',cardBill.id,ids,function(v){
						 	refreshPage()
						
				         })	
				  	}
			   	}	
			}
			function createRequestTable(){
				var columns=[];
					columns[0] ={type:"checkbox"}
				var button
				if(readOnly){
					button = {
				   		buttons:[]
				   	}
				}else{
					button = {
						buttons:[
				   			{value:"增 加", actions:addRequestBill},
				   			{value:"删 除", actions:deleteRequestBill}
				   		]
			   		}
				}	
			 	qt2 = new QueryTable("add_request_bill",null,"requestlist",columns,button);
			   	qt2.paramFunc = function(){return {cardPack:cardBill.pack}};
			   	qt2.showTable();
			   	cqtObj = qt2
			}
			function addRequestBill(){
			var ids = qt2.getBoxValue();
				var v = openDialogFrame("/store/add_request_order.html?cardType="+cardTypeId+"&cardTypeName="+cardTypeName+"&cardBillId="+cardBill.id+"&cardPackId="+cardBill.pack+"&destName="+dest.name+"&destId="+dest.id,'关联申请单',480,710)
				if(v){
					qt2.showTable();
				}
			}
			function deleteRequestBill(){
				var ids = qt2.getBoxValue();
				if(!ids){
			   		showWarning("请选择需要删除的申请单"); 
			   	}else{
			   	 	if(showQuestion("你确认要删除这些卡申请单?")==true){
					 	 ids =  ids.split(",")
			             DWR.call('cardStoreService.deleteRelatedRequestOrder',cardBill.id,ids,function(v){
						 	qt2.showTable();
				         })	
				  	}
			   	}	
			}
			function updata(){
				var v = openDialogFrame("/store/edit_bill_basic_info.html?cardBillId="+cardBill.id+"&billType="+cardBill.billType+"&remark="+cardBill.description,'修改单据基础信息',210,400)
				if (v){
					refreshPage()
				}
			}
			function operateLog(){
				openDialogFrame("/store/operation_log.html?orderId="+cardBill.id+"&logType="+Constants.LOG_TYPE_CARDBILL,'操作记录',455,700)
			}
			function selCardType(){			
				var v = openDialogFrame("/basic/card_type_list.html?",'请选择卡片类型',464,700)
				if (v){
					var form=$("detailedFrom")
				    form.cardTypeId.value=v[0];
					form.cardType.value = v[1]; 	 
				}
			}
			
			var initTab=true;
			function showTab(id, obj){
				if (!cqtObj.inited){
					return
				}
				switchPage(id);
				if(id == "requestlist" && initTab){
					createRequestTable();
					initTab = false;
				}
				
				$("tab_base").className = "treeTab";
    			$("tab_request").className = "treeTab";
    			if (typeof obj == "string") obj = $(obj)
    			obj.className = "treeTabc";
			
			}
			function onBack(){
				parent.onBack();
			}
			//入库方法
			function onCardIn(){
				if(cardBill.amount == 0){
					showInformation("入库数量不能小于零!");return;
				}
				if(!(showQuestion("确定入库吗?"))){
					return;
				}
				DWR.call("cardStoreService.confirmWareHousingAfterManufacture",cardBill.id,function(){
					showInformation("入库成功!");
					parent.printBill(cardBill)
					parent.switchPage("printBillDetail")
				},{blockScreen:"true"});
			}
			//出库方法
			function onCardOut(){
				if(cardBill.amount == 0){
					showInformation("出库数量不能为零!");return;
				}
				if(!(showQuestion("您确定出库吗?"))){
					return;
				}
				DWR.call("cardStoreService.confirmDelivery",cardBill.id,function(acquirer){
					showInformation("出库成功!");
					//Project.shopAutoInStore 是否自动入库
					if(Project.shopAutoInStore && acquirer.dest.orgLevel == 700){
						DWR.call("cardStoreService.doConfirmWareHousing",cardBill.id,acquirer.dest.id,function(){
						})
					}
					parent.printBill(cardBill)
					parent.switchPage("printBillDetail")
				},{blockScreen:"true"});
			}
			//提交方法
			function submit(){
				if(cardBill.amount == 0){
					showInformation("提交数量不能为零!");return;
				}
				if(!(showQuestion("您确定提交吗?"))){
					return;
				}
				DWR.call("cardStoreService.submitDeliveryCardBill",cardBill.id,function(){
					showInformation("提交成功!");
					onBack();;
				},{blockScreen:"true"});
			}
			//调整方法
			function onConfirm(){
				if(cardBill.amount == 0){
					showInformation("调整数量不能为零!");return;
				}
				if(!(showQuestion("确定调整库存吗?"))){
					return;
				}
				DWR.call("cardStoreService.confirmAdjustInventory",cardBill.id,function(){
					showInformation("库存调整成功!");
					parent.printBill(cardBill)
					parent.switchPage("printBillDetail")
				},{blockScreen:"true"});
			}

		</script>
	<style>
		.width100 .listBorder{
			margin:0px;
			width:100%;
		}
	</style>
</head>
<body onload="load()">
	<div id="cardBillDiv">
	<div id ="detailedView"></div>
	<div id="detailForm" style="width:95%">
		<div id='group_a' align="left">
			<a href='#' onclick="showTab('datalist',this);return false;" onfocus="this.blur()" tabindex="-1" id="tab_base" class="treeTabc">卡片号段</a>
			<a href='#'  onclick="showTab('requestlist',this);return false;" onfocus="this.blur()" tabindex="-1" id="tab_request" class="treeTab">关联申请单</a>
		</div>
		<div id="datalist"  class="width100"></div>
		<div id="requestlist" style="display:none;" class="width100"></div>
	</div>
	<p><span id="butCancel"></span> <button type="button" onclick="onBack()">返 回</button></p>
	</div>

</body>
</html>
